/** 企业管理- 招聘职位管理 model */
import { notification, message } from 'antd';
import { getTypeList } from '@/services/dictionary';
import {
    getPositionList,
    getRecruitInfo,
    getRecruitSchedule,
    getCandidateClaim,
    getCandidateInfo,
    postPpdateRecruit,
    getApply
} from '@/services/recrutManage/positionManage';

export default {
    namespace: 'positionManage',
    state: {
        // 招聘职位列表
        positionList: [],
        // 招聘进度字典
        recruitScheduleType: [
            { value: '1', text: '待开始11' },
            { value: '2', text: '招聘中22' }
        ],
        // 招聘进度筛选
        // schedule: '',
        // 表格排序
        orderBy: '',
        // 招聘职位列表分页
        positionPagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        // 职位信息详情
        recruitInfo: {},
        // 候选人要求详情
        candidateClaim: {},
        // 招聘进度详情
        recruitSchedule: {},
        // 候选人信息
        candidateInfo: [],
        candidatePagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        // 发起招聘流程需要的参数
        applyData: {}
    },
    effects: {
        *fetchList({ payload, dictionary }, { call, put }) {
            // const positionList = yield select(
            //     state => state.positionManage.positionList
            // );
            const result = yield call(getPositionList, payload, dictionary);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        positionList: result.data.rows,
                        positionPagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            } else {
                notification.error({
                    message: `请求错误 ${result.msg}`
                });
            }
        },
        *fetchApply({ payload }, { call, put }) {
            const result = yield call(getApply, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        applyData: result.data
                    }
                });
            } else {
                notification.destroy();
                notification.error({
                    message: `请求错误 ${result.msg}`
                });
            }
        },
        *fetchTypeList({ payload }, { call, put }) {
            const result = yield call(getTypeList, payload);
            const { data } = result;
            if (result && result.code === 10000) {
                const newData = [];
                data.forEach(item => {
                    newData.push({
                        value: item.dictValue,
                        text: item.dictName
                    });
                });
                yield put({
                    type: 'save',
                    payload: {
                        recruitScheduleType: newData
                    }
                });
            } else {
                notification.destroy();
                notification.error({
                    message: `请求错误 ${result.msg}`
                });
            }
        },
        *fetchDetail({ payload, pathName }, { put, all }) {
            yield all([
                put({
                    type: 'fetchRecruitInfo',
                    payload,
                    dictionary: {
                        pathName,
                        fetchType: 'fetchRecruitInfo'
                    }
                }),
                put({
                    type: 'fetchCandidateClaim',
                    payload,
                    dictionary: {
                        pathName,
                        fetchType: 'fetchCandidateClaim'
                    }
                }),
                put({
                    type: 'fetchRecruitSchedule',
                    payload,
                    dictionary: {
                        pathName,
                        fetchType: 'fetchRecruitSchedule'
                    }
                }),
                put({
                    type: 'fetchCandidateInfo',
                    payload
                })
            ]);
        },
        /** 职位信息 */
        *fetchRecruitInfo({ payload, dictionary }, { call, put }) {
            const result = yield call(getRecruitInfo, payload, dictionary);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        recruitInfo: result.data
                    }
                });
            }
        },
        /** 候选人要求 */
        *fetchCandidateClaim({ payload, dictionary }, { call, put }) {
            const result = yield call(getCandidateClaim, payload, dictionary);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candidateClaim: result.data
                    }
                });
            }
        },
        /** 招聘进度 */
        *fetchRecruitSchedule({ payload, dictionary }, { call, put }) {
            const result = yield call(getRecruitSchedule, payload, dictionary);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        recruitSchedule: result.data
                    }
                });
            }
        },
        /** 候选人信息 */
        *fetchCandidateInfo({ payload }, { call, put }) {
            const result = yield call(getCandidateInfo, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        candidateInfo: result.data.rows
                    }
                });
            }
        },
        /**
         * 招聘任务编辑
         * dictionary:{pathName,fetchType}
         * */
        *updateRecruit({ payload, dictionary }, { call }) {
            const result = yield call(postPpdateRecruit, payload, dictionary);
            if (result && result.code === 10000) {
                message.success(result.msg);
            } else {
                message.error(result.msg);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear(state) {
            return {
                ...state,
                // 招聘职位列表
                positionList: [],
                // 招聘进度字典
                recruitScheduleType: [
                    { value: '1', text: '待开始11' },
                    { value: '2', text: '招聘中22' }
                ],
                // 招聘进度筛选
                schedule: '',
                // 表格排序
                orderBy: '',
                // 招聘职位列表分页
                positionPagination: {
                    current: 1,
                    pageSize: 10,
                    total: 0
                },
                // 职位信息详情
                recruitInfo: {},
                // 候选人要求详情
                candidateClaim: {},
                // 招聘进度详情
                recruitSchedule: {},
                // 候选人信息
                candidateInfo: [],
                candidatePagination: {
                    current: 1,
                    pageSize: 10,
                    total: 0
                },
                // 发起招聘流程需要的参数
                applyData: {}
            };
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            const { location } = history;
            const { schedule = '' } = location.query;
            if (schedule) {
                dispatch({ type: 'save', payload: { schedule } });
            }
        }
    }
};
